// package-store/pages/store-spokes-setting/index.js
const app = getApp();
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { PACKAGE_STORE_PAGE } from '../../../constants/route';
import { navBack, navToAction, tipToast, tipSuccess, showLoading, hideLoading } from '../../../common/env';
import { obj2Arr, formatPrice } from '../../../common/utils';
import { APPLY_STRATIFIED } from '../../../constants/manage';
import { getEndorsement, setEndorsement } from '../../../api/sto';
let loaded = false;

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        settingInfo: {},
        userStratifiedPickerPopup: {
            show: false,
            columns: obj2Arr(APPLY_STRATIFIED),
            map: APPLY_STRATIFIED,
        },
        consumerLowerExpenseModal: {
            show: false,
        },
        spokesNameModal: {
            show: false,
        },
    },
    onLoad(options) {
        const { isOpen } = options;
        this._handleGetDetail(isOpen);
        app.event.addListener('getSpokesSettingInfo', this._handleGetSettingInfo.bind(this));
    },
    onUnload() {
        app.event.removeListener('getSpokesSettingInfo');
    },
    async _handleGetDetail(isOpen) {
        !loaded && showLoading('加载中...');
        const settingInfo = await getEndorsement();
        if (isOpen) {
            settingInfo.isOpen = true;
        }
        this.setData({ settingInfo });
        if (!loaded) hideLoading(); loaded = true;
    },
    _handleGetSettingInfo(settingInfo) {
        this.setData({ settingInfo });
    },
    handleSwitchChange(e) {
        const { name } = e.currentTarget.dataset;
        const value = e.detail;
        switch (name) {
            case 'isOpen':
                this.setData({ 'settingInfo.isOpen': value });
                break;
            case 'isEntranceDisplay':
                this.setData({ 'settingInfo.isEntranceDisplay': value });
                break;
            case 'isAutoApproved':
                this.setData({ 'settingInfo.isAutoApproved': value });
                break;
            default:
                break;
        }
    },
    handleChangeSpokesName(e) {
        this.setData({
            'settingInfo.spokesmanTitle': e.detail,
        });
    },
    handleChangeUserStratified(e) {
        const { value } = e.detail.value;
        this.setData({
            'settingInfo.userStratified': value,
        });
    },
    handleChangeConsumerLowerExpense(e) {
        this.setData({
            'settingInfo.consumerLowerExpense': formatPrice(e.detail),
        });
    },
    handleShowSpokesNameModal() {
        this.setData({
            'spokesNameModal.show': true,
        });
    },
    handleShowUserStratifiedPickerPopup() {
        this.setData({
            'userStratifiedPickerPopup.show': true,
        });
    },
    handleShowConsumerLowerExpenseModal() {
        this.setData({
            'consumerLowerExpenseModal.show': true,
        });
    },
    handleNavTo() {
        const { settingInfo } = this.data;
        navToAction(PACKAGE_STORE_PAGE['storeSpokesSettingApply'], null, {
            success(res) {
                setTimeout(() => {
                    app.event.trigger('sendSpokesSettingInfo', settingInfo);
                }, 800);
            },
        });
    },
    async handleSave() {
        const { settingInfo } = this.data;
        settingInfo.needApplyInfo = true;
        if (!settingInfo.spokesmanTitle) {
            tipToast('请设置代言人名称');
            return;
        } else if (!settingInfo.userStratified) {
            tipToast('请设置代言人申请条件');
            return;
        } else if (settingInfo.userStratified === 'consumer' && !settingInfo.consumerLowerExpense) {
            tipToast('请设置最低消费金额');
            return;
        } else if (!settingInfo.applyInfoFields) {
            tipToast('请设置申请信息');
            return;
        }
        showLoading('保存中...');
        const flag = await setEndorsement(settingInfo);
        if (flag) {
            hideLoading();
            tipSuccess('创建成功');
            navBack();
        }
    },
})
